﻿namespace com.stifel.ProjectMgmt
{
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;

    using com.stifel.Extensions.StringExtensions;

    public partial class CalendarDashboardUserControl : UserControl
    {
        #region Methods

        protected void Page_Load( object sender, EventArgs e )
        {
            if ( !Page.IsPostBack )
            {
                loadDefaults( );
                loadYearMonth( );
                setUrlReferrer( );
                load( );
            }
        }

        protected void uiBack_Click( object sender, ImageClickEventArgs e )
        {
            int i = uiYear.SelectedIndex - 1;
            uiYear.SelectedIndex = i;
            setArrowsState( );
            load( );
        }

        protected void uiForward_Click( object sender, ImageClickEventArgs e )
        {
            int i = uiYear.SelectedIndex + 1;
            uiYear.SelectedIndex = i;
            setArrowsState( );
            load( );
        }

        protected void uiToday_Click( object sender, EventArgs e )
        {
            DateTime today = DateTime.Now;
            int i = -1;
            foreach ( ListItem item in uiYear.Items )
            {
                i++;
                string[ ] s = item.Value.Split( '\t' );
                int y = s[ 0 ].ToInt32( );
                int m = s[ 1 ].ToInt32( );
                if ( today.Year.Equals( y ) && today.Month.Equals( m ) )
                    break;
            }
            uiYear.SelectedIndex = i;
            setArrowsState( );
            load( );
        }

        protected void uiYear_SelectedIndexChanged( object sender, EventArgs e )
        {
            load( );
        }

        void setArrowsState( )
        {
            int x = uiYear.SelectedIndex;
            if ( x <= 0 )
                uiBack.Enabled = false;
            else
                uiBack.Enabled = true;
            if ( x >= uiYear.Items.Count - 1 )
                uiForward.Enabled = false;
            else
                uiForward.Enabled = true;
        }

        #endregion Methods
    }
}